import singleCpu
import multiprocessing as mp

flag = True

def worker(id,savepage,reproduce):
    try:
        status = singleCpu.singleCpu(savepage = savepage,reproduce = reproduce)
        print('Task ' + str(id) +' complete')
    except Exception as e:
        print(e)
    if status == False:
        global flag
        flag = False

def main():
    sp = input('Save pages?(y/n)')
    rp = input('Reproduce datas?(y/n)')
    stop = input('Unlimited mode?(y/n)')
    if stop == 'y' or stop == 'Y':
        Lock = False
        t = 0xffff
    else:
        Lock = True
        t = eval(input('Repte Times:'))
    if sp == 'y' or sp == 'Y':
        savepage = True
    else:
        savepage = False
    if rp == 'y' or rp == 'Y':
        reproduce = True
    else:
        reproduce = False
    cnt = 0    
    while cnt <= t:
        if Lock:
            cnt += 1
        if flag == False:
            break
        processes = []
        for i in range(8):
            process = mp.Process(target=worker, args=(i,savepage,reproduce), name=f"Process-{i}")
            processes.append(process)
            process.start()
    
        for process in processes:
            process.join()

if __name__ == "__main__":
    main()
